<?php
namespace app\common\model;
use think\Model;
use app\common\model\Devs_share;
class User extends Model
{
    /**
     * 增
     * @param mixed|string $data
     * @return int|string
     */
    public function add($data){
        return self::insert($data);
    }

    /**
     * 写入数据
     * @param  [array] $data 
     * @return [type]       [返回自增ID]
     */
    public function inGetData($data){
        return self::insertGetId($data);
    }

    /**
     * 排序全部查询
     * @param $where
     * @param $pageSize
     * @param array $pageParam
     * @return \think\Paginator
     */
    public function getAll($where,$order,$pageSize,$pageParam=[]){
        $result=self::order($order);
        if ($where){
            $result->where($where);
        }
        return $result->paginate($pageSize,false,$pageParam);

    }

    /**
     * 获取列表
     * @param int $end 结束时间
     * @return array
     * */
    public function getlists($where=[],$field="*"){
        $result = self::field($field);
        
        if ($where){
            $result = $result->where($where);
        }
        $result = $result->select();

        return $result;
    }

    /**
     * 删
     * @param $id
     * @return int
     */
    public function del($where){
        return self::where($where)->delete();
    }


    /**
     * 获取某字段信息
     */
    public function getOne($where,$field="*"){
        $result = self::field($field)->where($where)->find();
        if($result){
            $result = $result->getData();
        }
        return $result;
    }

    /**
     * 更改字段信息
     * @param $id
     * @param $data
     * @return $this
     */
    public function updateFieldsById($where,$data){
        return self::where($where)->update($data);
    }

    /**
     * 修改mysql警报推送方式
     * @param int $uid 用户uid
     * @return boolean
     */
    public function changeMysqlAlarmType($uid, $is_alert, $type){
        $save['is_alert'] = $is_alert;
        
        if($type){
            $save['alert_type'] = $type;
        }
        
        $flag = self::where(array('uid' => $uid))->save($save);
        
        if($flag === false){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 注册身份验证
     * @return [type] [description]
     */
    public function userCheck($res){
            //身份
            $place = config('PLACE');

            $path = ROOT_PATH . 'public' . DS . 'uploads';
            $data = array();

            $data['name'] = $res['name'];
            $type = explode(",",$res['type']);

            //头像
            $file1 = request()->file('photo');
            if($file1){
                $fileName1 = 'imgs'. DS .time().rand(1,999);
                $info1 = $file1->move($path,$fileName1);
                if($info1){
                    $data['photo'] = $info1->getSaveName();
                }else{
                    // 上传失败返回错误信息
                    $this->error($file1->getError());

                    return ['state'=>false,'msg'=>'头像上传失败'];
                }
            }

            if(count($type)==1){
                if($place['PLACE_RMAN_AYS']['id'] == $type[0]){
                    
                    $tuser = $this->getOne('uid',['phone'=>$res['tel']]);
                    if($tuser){
                        return ['state'=>false,'msg'=>'推荐人不存在'];
                    }
                    //匿名资源人
                    $data['t2']=time();
                    return ['state'=>true,'msg'=>$data];
                }
            }

            $data['company'] = isset($res['company'])?$res['company']:'';
            $data['pid'] = isset($res['pid'])?$res['pid']:'';
            $data['cid'] = isset($res['cid'])?$res['cid']:'';
            $data['aid'] = isset($res['aid'])?$res['aid']:'';
            $data['phone'] = isset($res['phone'])?$res['phone']:'';
            $data['email'] = isset($res['email'])?$res['email']:'';

            $note['idcode'] = isset($res['idcode'])?$res['idcode']:'';
            $note['wcard'] = isset($res['wcard'])?$res['wcard']:"";
            $note['sex'] = isset($res['sex'])?$res['sex']:'';

            //资源人
            if(in_array($place['PLACE_RMAN_OPEN']['id'],$type) || in_array($place['PLACE_RMAN_AYS']['id'],$type)){
                $tuser = $this->getOne('uid',['phone'=>$res['tel']]);
                if(!$tuser){
                    return ['state'=>false,'msg'=>'推荐人不存在'];
                }
            }

            //身份证正面
            $file2 = request()->file('id_just');
            if($file2){
                $fileName2 = 'user'. DS .time().rand(1,999);
                $info2 = $file2->move($path,$fileName2);
                if($info2){
                    $note['id_just'] = $info2->getSaveName();
                }else{
                    // 上传失败返回错误信息
                    return ['state'=>false,'msg'=>'身份证正面上传失败'];
                }
            }else{
                return ['state'=>false,'msg'=>'请上传身份证正面'];
            }

            //身份证反面
            $file3 = request()->file('id_back');
            if($file3){
                $fileName3 = 'user'. DS .time().rand(1,999);
                $info3 = $file3->move($path,$fileName3);
                if($info3){
                    $note['id_back'] = $info3->getSaveName();
                }else{
                    // 上传失败返回错误信息
                    return ['state'=>false,'msg'=>'身份证背面上传失败'];
                }
            }else{
                return ['state'=>false,'msg'=>'请上传身份证背面'];
            }

            //合作伙伴 
            if(in_array($place['PLACE_CPER_ONE']['id'],$type) || in_array($place['PLACE_AGEN']['id'],$type)){
                //合约
                $file1 = request()->file('contract_1');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $note['contract_1'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'合约上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传合约'];
                }
            }

            //代理商  
            if(in_array($place['PLACE_AGC_P']['id'],$type) || in_array($place['PLACE_AGC_C']['id'],$type) || in_array($place['PLACE_AGC_A']['id'],$type)){
                //合约
                $file1 = request()->file('contract_2');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $note['contract_2'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                         return ['state'=>false,'msg'=>'合约上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传代理商合约'];
                }

                //营业执照
                $file1 = request()->file('license_2');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $note['license_2'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        $this->error('请上传营业执照');
                        return ['state'=>false,'msg'=>'代理商营业执照上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传代理商营业执照'];
                }
            }

            //外部工程师
            if(in_array($place['PLACE_ABD_ENG']['id'],$type)){
                $file1 = request()->file('cert_4');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $note['cert_4'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'工程师证书上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传工程师证书'];
                }
            }

            //单体工程师
            if(in_array($place['PLACE_MER_ENG']['id'],$type)){
                $file1 = request()->file('cert_d');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $note['cert_d'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'单体工程师证书上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传单体工程师证书'];
                }
            }

            //中央工程师
            if(in_array($place['PLACE_HAN_ENG']['id'],$type)){
                $file1 = request()->file('cert_z');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $note['cert_z'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'中央工程师证书上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传中央工程师证书'];
                }
            }

            //冷库工程师
            if(in_array($place['PLACE_CTE_ENG']['id'],$type)){
                $file1 = request()->file('cert_l');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $note['cert_l'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        $this->error('冷库工程师证书上传失败');
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传冷库工程师证书'];
                }
            }

            //技术人员
            if(in_array($place['PLACE_ABD_THY']['id'],$type)){
                //证书
                $file1 = request()->file('cert_2');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $note['cert_2'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'技术证书上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传技术证书'];
                }
            }

            //维保人员
            if(in_array($place['PLACE_MTES']['id'],$type)){
                $file1 = request()->file('cert_3');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $note['cert_3'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'维保人员证书上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传维保人员证书'];
                }
            }

            //外包工程商
            if(in_array($place['PLACE_O_MANS']['id'],$type) || in_array($place['PLACE_T_MANS']['id'],$type) || in_array($place['PLACE_S_MANS']['id'],$type)){
                //营业执照
                $file1 = request()->file('license_1');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $note['license_1'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'营业执照上传失败'];
                    }
                }else{
                     return ['state'=>false,'msg'=>'请上传营业执照'];
                }

                //合同
                $file1 = request()->file('contract_3');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $note['contract_3'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'外包工程商合同上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传外包工程商合同'];
                }
            }

            $data['note'] = json_encode($note);

            $data['t2']=time();

            return ['state'=>true,'msg'=>$data];
    }

    /**
     * 身份
     * @return [type] [description]
     */
    public function userUpd($res,$data){
            //身份
            $place = config('PLACE');

            $path = ROOT_PATH . 'public' . DS . 'uploads';

            $type = explode(",",$res['type']);

            if(count($type)==1){
                if($place['PLACE_RMAN_AYS']['id'] == $type[0]){
                    
                    $tuser = $this->getOne('uid',['phone'=>$res['tel']]);
                    if(!$tuser){
                        return ['state'=>false,'msg'=>'推荐人不存在'];
                    }
                    //匿名资源人
                    $data['t2']=time();
                    return ['state'=>true,'msg'=>$data];
                }
            }

            if(isset($res['name'])){
                $data['name'] = $res['name'];
            }

            if(isset($res['company'])){
                $data['company'] = $res['company'];
            }

            if(isset($res['pid'])){
                $data['pid'] = $res['pid'];
            }

            if(isset($res['cid'])){
                $data['cid'] = $res['cid'];
            }

            if(isset($res['aid'])){
                $data['aid'] = $res['aid'];
            }

            if(isset($res['phone'])){
                $data['phone'] = $res['phone'];
            }

            if(isset($res['email'])){
                $data['email'] = $res['email'];
            }

            if(isset($res['idcode'])){
                $data['note']['idcode'] = $res['idcode'];
            }

            if(isset($res['wcard'])){
                $data['note']['wcard'] = $res['wcard'];
            }

            if(isset($res['sex'])){
                $data['note']['sex'] = $res['sex'];
            }

            //资源人
            if(in_array($place['PLACE_RMAN_OPEN']['id'],$type) || in_array($place['PLACE_RMAN_AYS']['id'],$type)){
                $tuser = $this->getOne('uid',['phone'=>$res['tel']]);
                if(!$tuser){
                    return ['state'=>false,'msg'=>'推荐人不存在'];
                }
            }

            //身份证正面
            $file2 = request()->file('id_just');
            if($file2){
                $fileName2 = 'user'. DS .time().rand(1,999);
                $info2 = $file2->move($path,$fileName2);
                if($info2){
                    $data['note']['id_just'] = $info2->getSaveName();
                }else{
                    // 上传失败返回错误信息
                    return ['state'=>false,'msg'=>'身份证正面上传失败'];
                }
            }

            //身份证反面
            $file3 = request()->file('id_back');
            if($file3){
                $fileName3 = 'user'. DS .time().rand(1,999);
                $info3 = $file3->move($path,$fileName3);
                if($info3){
                    $data['note']['id_back'] = $info3->getSaveName();
                }else{
                    // 上传失败返回错误信息
                    return ['state'=>false,'msg'=>'身份证背面上传失败'];
                }
            }

            //合作伙伴 
            if(in_array($place['PLACE_CPER_ONE']['id'],$type) || in_array($place['PLACE_AGEN']['id'],$type)){
                //合约
                $file1 = request()->file('contract_1');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $data['note']['contract_1'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'合约上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传合约'];
                }
            }

            //代理商  
            if(in_array($place['PLACE_AGC_P']['id'],$type) || in_array($place['PLACE_AGC_C']['id'],$type) || in_array($place['PLACE_AGC_A']['id'],$type)){
                //合约
                $file1 = request()->file('contract_2');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $data['note']['contract_2'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                         return ['state'=>false,'msg'=>'合约上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传代理商合约'];
                }

                //营业执照
                $file1 = request()->file('license_2');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $data['note']['license_2'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        $this->error('请上传营业执照');
                        return ['state'=>false,'msg'=>'代理商营业执照上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传代理商营业执照'];
                }
            }

            //外部工程师
            if(in_array($place['PLACE_ABD_ENG']['id'],$type)){
                $file1 = request()->file('cert_4');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $data['note']['cert_4'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'工程师证书上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传工程师证书'];
                }
            }

            //单体工程师
            if(in_array($place['PLACE_MER_ENG']['id'],$type)){
                $file1 = request()->file('cert_d');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $data['note']['cert_d'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'单体工程师证书上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传单体工程师证书'];
                }
            }

            //中央工程师
            if(in_array($place['PLACE_HAN_ENG']['id'],$type)){
                $file1 = request()->file('cert_z');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $data['note']['cert_z'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'中央工程师证书上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传中央工程师证书'];
                }
            }

            //冷库工程师
            if(in_array($place['PLACE_CTE_ENG']['id'],$type)){
                $file1 = request()->file('cert_l');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $data['note']['cert_l'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        $this->error('冷库工程师证书上传失败');
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传冷库工程师证书'];
                }
            }

            //技术人员
            if(in_array($place['PLACE_ABD_THY']['id'],$type)){
                //证书
                $file1 = request()->file('cert_2');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $data['note']['cert_2'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'技术证书上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传技术证书'];
                }
            }

            //维保人员
            if(in_array($place['PLACE_MTES']['id'],$type)){
                $file1 = request()->file('cert_3');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $data['note']['cert_3'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'维保人员证书上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传维保人员证书'];
                }
            }

            //外包工程商
            if(in_array($place['PLACE_O_MANS']['id'],$type) || in_array($place['PLACE_T_MANS']['id'],$type) || in_array($place['PLACE_S_MANS']['id'],$type)){
                //营业执照
                $file1 = request()->file('license_1');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $data['note']['license_1'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'营业执照上传失败'];
                    }
                }else{
                     return ['state'=>false,'msg'=>'请上传营业执照'];
                }

                //合同
                $file1 = request()->file('contract_3');
                if($file1){
                    $fileName1 = 'user'. DS .time().rand(1,999);
                    $info1 = $file1->move($path,$fileName1);
                    if($info1){
                        $data['note']['contract_3'] = $info1->getSaveName();
                    }else{
                        // 上传失败返回错误信息
                        return ['state'=>false,'msg'=>'外包工程商合同上传失败'];
                    }
                }else{
                    return ['state'=>false,'msg'=>'请上传外包工程商合同'];
                }
            }

            $data['note'] = json_encode($data['note']);
            return ['state'=>true,'msg'=>$data];
    }

    /**
     * 生成指用户的通知地址.若指定设备号,则生成通知地址包含有效时间段
     * @access public
     * @param int $uid
     * @param string/void $dev_id
     * @return array 
     * */
    public function produceUserAddr($uid, $dev_id = false){
        if($uid=="-1"){
            return false;
        }
        $adr['app'] = $uid;
        //将报警地址添加到mysql中
        $res = $this->getOne('phone,name,third_id,alert_type,is_alert',['uid'=>$uid]);
        if($res['phone']) {
            $adr['sms'] = $res['phone'];
        }
        if($res['name']) {
            $adr['email'] = $res['name'];
        }
        if($res['third_id']) {
            $adr['weixin'] = $res['third_id'];
        }
        $adr['alert_type'] = $res['alert_type']?$res['alert_type']:"";
        $adr['is_alert'] = $res['is_alert'];
        
        if($dev_id){
            //共享起止时间
            $shareModel = new Devs_shar();
            $row = $shareModel->getOne(['guid1'=>$dev_id,'share_with'=>$uid],'st,et');
            if($row) {
                $adr['st'] = $row['st'];
                $adr['et'] = $row['et'];
            }
        }else {
            //用户自己的设备
            $adr['st'] = 0;
            $adr['et'] = 0;
        }
        
        return $adr;
    }


    /**
     * 用户信息
     * @param  integer $id 用户id
     * @return array
     */
    public function info($id)
    {
        $data = User::get($id);
        if ($data) {
            return $data->getData();
        }
        return null;
    }


    /**
     * 搜索用户
     * @param  string $name 用户名
     * @return array
     */
    public function search($name)
    {
        if (strlen($name) === 0) {
            return null;
        }

        $res = $this->where(['name' => ['like', '%' . $name . '%']])
            ->field('uid,name,phone')
            ->select();

        return endoceArr($res);
    }

    /**
     * 用户地理统计
     * @param  string $field 字段
     * @param  array $data  用户类型
     * @return array        统计结果
     */
    public function counts_user_by_city($field, $data){
        foreach ($data as $v) {
            $result=self::field("COUNT(*) num")
                ->where([
                     'valid'=>1,
                     $field=>$v,
                ])
                ->count();
            $res[$v] =$result;
        }
        return $res;

    }


}